import java.util.HashSet;

public class Demo {
    public int lengthOfLongestSubstring(String s){
        int len = 0;
        int left = 0;            //a b c a b c b b
        int right = 0;
        int newlen = 0;
        int length = s.length();
        HashSet<Character> set = new HashSet<>();
        if (s.length() == 0){
            return 0;
        }
        for (right = 0; right < length;right++) {
            while (set.contains(s.charAt(right))){
                newlen = Math.max(newlen,len);
                set.remove(s.charAt((left)));
                left++;
                len--;
            }
            if(!set.contains(s.charAt(right))){
                set.add(s.charAt(right));
                len++;
            }
        }
        return Math.max(newlen,len);
    }
}
